/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.common.redis.utils.RedisUtils;
import com.comac.ins.isfd.constant.RedisConstants;
import com.comac.ins.isfd.constant.enums.IsfdSectionTypeEnum;
import com.comac.ins.isfd.domain.IsfdDatabaseDataImportRecord;
import com.comac.ins.isfd.domain.IsfdDatabaseDataImportSubRecord;
import com.comac.ins.isfd.domain.IsfdDatabaseTypeManagement;
import com.comac.ins.isfd.domain.bo.*;
import com.comac.ins.isfd.domain.bo.query.*;
import com.comac.ins.isfd.domain.vo.*;
import com.comac.ins.isfd.factory.IsfdSectionFactory;
import com.comac.ins.isfd.mapper.*;
import com.comac.ins.isfd.service.*;
import com.comac.ins.system.domain.vo.SysDictDataVo;
import com.comac.ins.system.service.ISysDictTypeService;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Semaphore;

/**
 *
 * @date: 2025/7/7 17:28
 **/
@Service
@Slf4j
public class IsfdNextPlatformServiceImpl implements IsfdNextPlatformService {
    @Autowired
    private IsfdModelServiceImpl isfdModelServiceImpl;
    @Autowired
    private IIsfdDatabaseMongoService mongoService;
    @Autowired
    private IIsfdMeshModelService iIsfdMeshModelService;
    @Autowired
    private IsfdCompositeModelService IIsfdModelService;
    @Autowired
    private IsfdFluidModelService IsfdFluidModelService;
    @Autowired
    private  IIsfdLoadService isfdLoadService;
    @Autowired
    private IIsfdCalculationExampleService IIsfdCalculationExampleService;
    @Autowired
    private IsfdSectionFactory isfdSectionFactory;
    @Autowired
    private  IIsfdLayupSkinPropertieService isfdLayupSkinPropertieService;
    @Autowired
    private  IIsfdLayupSkinPropertieMongoService isfdLayupSkinPropertieMongoService;
    @Autowired
    private  IIsfdRegionLibraryMongoService isfdRegionLibraryMongoService;
    @Autowired
    private  IsfdMetalMaterialMapper isfdMetalMaterialMapper;
    @Autowired
    private  IsfdCompositeMaterialMapper isfdCompositeMaterialMapper;
    @Autowired
    private IFastenerService iFastenerService;
    @Autowired
    private com.comac.ins.isfd.service.IIsfdResultService IIsfdResultService;
    @Autowired
    private IIsfdAnalysisService iIsfdAnalysisService;
    @Autowired
    private IIsfdOperatorService iIsfdOperatorService;
    @Autowired
    private IIsfdParameterConfigService iIsfdParameterConfigService;
    @Autowired
    private  ISysDictTypeService dictTypeService;
    @Autowired
    private IsfdDatabaseTypeManagementMapper isfdDatabaseTypeManagementMapper;
    @Autowired
    private IsfdDatabaseDataImportRecordMapper isfdDatabaseDataImportRecordMapper;
    @Autowired
    private IsfdDatabaseDataImportSubRecordMapper isfdDatabaseDataImportSubMapper;
    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
    @Autowired
    private IIsfdMeshOp2Service iIsfdMeshOp2Service;

    private final int THREAD_POOL_SIZE = Runtime.getRuntime().availableProcessors();
    @Override
    public IsfdModeCardlVo getModelcards() {
        IsfdModeCardlVo isfdModeCardlVo = new IsfdModeCardlVo();
        List<SysDictDataVo> data = dictTypeService.selectDictDataByType("isfd_domain");
        String domain = "";
        if(data.size()>0){
            domain = data.get(0).getDictValue();
        }
        List<IsfdModeCardlVo.ModelCard> modelCards = new ArrayList<>();
        PageQuery pageQuery =  new PageQuery();
        pageQuery.setPageNum(1);
        pageQuery.setPageSize(10);
        // 查询材料库-结构分析资产
        IsfdModeCardlVo.ModelCard meterialCard = new IsfdModeCardlVo.ModelCard();
        meterialCard.setDataBaseName("材料库-结构分析资产");
        meterialCard.setLinkUrl(domain+"/isfd/StrengthDataAssets/material");
        meterialCard.setDataBaseDes("结构分析资产-材料库主要记录所有固体材料及其模量、泊松比、密度、热膨胀系数等材料性质；记录所有材料密度、黏度、压缩模量、热传导系数等材料性质。");
        Long count1 = isfdMetalMaterialMapper.selectCount(null);
        Long count2 =isfdCompositeMaterialMapper.selectCount(null);
        meterialCard.setDataCount(mongoService.count("mat_material")+ mongoService.count("comp_material")+count1+count2);
        modelCards.add(meterialCard);
        //查询材料库-数据资产
        IsfdModeCardlVo.ModelCard meterialCard1 = new IsfdModeCardlVo.ModelCard();
        meterialCard1.setDataBaseName("材料库-数据资产");
        meterialCard1.setLinkUrl(domain+"/isfd/DataAssets/material2");
        meterialCard1.setDataBaseDes("数据资产-材料库主要记录所有固体材料及其模量、泊松比、密度、热膨胀系数等材料性质");
        meterialCard1.setDataCount(getDataCountFromRecord("材料库"));
        modelCards.add(meterialCard1);
        //查询模型库-结构分析资产
        IsfdModeCardlVo.ModelCard modelCard = new IsfdModeCardlVo.ModelCard();
        modelCard.setDataBaseName("模型库-结构分析资产");
        modelCard.setLinkUrl(domain+"/isfd/StrengthDataAssets/modal");
        modelCard.setDataBaseDes("结构分析资产-模型库描述模型的几何形状、连通关系（结构），用于分析求解的完整模型，主要由用户手动构建或导入。");
        TableDataInfo<IsfdModelVo> pages = isfdModelServiceImpl.queryPageList(new IsfdModelBo(), pageQuery);
        IsfdMeshQueryBo isfdMeshQueryBo = new IsfdMeshQueryBo();
        isfdMeshQueryBo.setPageQuery(pageQuery);
        TableDataInfo<Document> meshs = iIsfdMeshModelService.queryList(isfdMeshQueryBo);
        modelCard.setDataCount(pages.getTotal()+meshs.getTotal());
        modelCards.add(modelCard);
        //查询模型库-复合材料设计
        IsfdModeCardlVo.ModelCard modelCard1 = new IsfdModeCardlVo.ModelCard();
        modelCard1.setDataBaseName("模型库-复合材料设计");
        modelCard1.setLinkUrl(domain+"/isfd/CompositeMaterial/model-library");
        modelCard1.setDataBaseDes("复合材料设计-模型库描述模型的几何形状、连通关系（结构），用于分析求解的完整模型，主要由用户手动构建或导入。");
        TableDataInfo<IsfdCompositeModelVo> compositeModels = IIsfdModelService.queryPageList(new IsfdModelBo(), pageQuery);
        modelCard1.setDataCount(compositeModels.getTotal());
        modelCards.add(modelCard1);
        //查询流体模型库
        IsfdModeCardlVo.ModelCard modelCard2 = new IsfdModeCardlVo.ModelCard();
        modelCard2.setDataBaseName("流体模型库-流体分析");
        modelCard2.setLinkUrl(domain+"/isfd/fluid/fluid_model");
        modelCard2.setDataBaseDes("流体模型库描述模型的几何形状、连通关系（结构）和流动行为（流体），用于分析求解的完整模型，主要由用户手动构建或导入。");
        TableDataInfo<IsfdFluidModelVo> fluidModels = IsfdFluidModelService.queryPageList(new IsfdFluidModelBo(),pageQuery);
        modelCard2.setDataCount(fluidModels.getTotal());
        modelCards.add(modelCard2);
        //查询紧固件库数据
        IsfdModeCardlVo.ModelCard fasterCard = new IsfdModeCardlVo.ModelCard();
        fasterCard.setDataBaseName("紧固件库-结构分析资产");
        fasterCard.setLinkUrl(domain+"/isfd/StrengthDataAssets/fastener");
        fasterCard.setDataBaseDes("结构分析资产-紧固件库记录所有紧固件的几何信息、材料信息、拉伸剪切力学性能等。主要来源于外界标准库和采购标准。");
        List<FastenerCountObjectVo> fastenerCountObjectVoList = iFastenerService.listCount();
        // 计算紧固件库数据总数
        long totalCount = fastenerCountObjectVoList.stream()
            .mapToLong(FastenerCountObjectVo::getCount)
            .sum();
        fasterCard.setDataCount(totalCount);
        modelCards.add(fasterCard);
        //查询紧固件库数据
        IsfdModeCardlVo.ModelCard fasterCard1 = new IsfdModeCardlVo.ModelCard();
        fasterCard1.setDataBaseName("紧固件库-数据资产");
        fasterCard1.setLinkUrl(domain+"/isfd/DataAssets/faster");
        fasterCard1.setDataBaseDes("数据资产-紧固件库记录所有紧固件的几何信息、材料信息、拉伸剪切力学性能等。主要来源于外界标准库和采购标准。");
        fasterCard1.setDataCount(getDataCountFromRecord("紧固件库"));
        modelCards.add(fasterCard1);
        //查询紧结果库数据
        IsfdModeCardlVo.ModelCard resultCard = new IsfdModeCardlVo.ModelCard();
        resultCard.setDataBaseName("结果库-结构分析资产");
        resultCard.setLinkUrl(domain+"/isfd/StrengthDataAssets/result");
        resultCard.setDataBaseDes("结构分析资产-结果库模块支持结构应力分布、位移变形、变形刚度等结构计算结果以及流体速度分布、压力分布、温度分布等流体计算结果的数据管理。模块能够按照标准模板导入并解析这些计算结果数据。");
        TableDataInfo<IsfdResultVo> results = IIsfdResultService.queryPageList(new IsfdResultBo(),pageQuery);
        IsfdMeshQueryBo isfdMeshQueryBo1 = new IsfdMeshQueryBo();
        isfdMeshQueryBo1.setPageQuery(pageQuery);
        TableDataInfo<Document> meshResults = iIsfdMeshOp2Service.queryList(isfdMeshQueryBo1);
        resultCard.setDataCount(results.getTotal()+meshResults.getTotal());
        modelCards.add(resultCard);
        //查询算法库数据
        IsfdModeCardlVo.ModelCard algorithmCard = new IsfdModeCardlVo.ModelCard();
        algorithmCard.setDataBaseName("算法库（算法）-结构分析资产");
        algorithmCard.setLinkUrl(domain+"/isfd/StrengthDataAssets/algorithm/algorithm-definition");
        algorithmCard.setDataBaseDes("结构分析资产-算法库模块支持定义算法数据结构与模板配置，能够按标准模板导入并解析各类数值方法、网格划分算法、优化算法、AI算法及统计算法等数据。模块不仅提供用户批量导入算法数据的功能，还统一集成管理算法库中的头文件、公式、计算库、函数代码及参数文件，并支持算法库数据的调用查询、批量导入等，从而确保算法资源的高效管理和便捷使用。");
        TableDataInfo<IsfdAnalysisVo> analysisPageInfo = iIsfdAnalysisService.queryPageList(new IsfdAnalysisQueryBo());
        algorithmCard.setDataCount(analysisPageInfo.getTotal());
        modelCards.add(algorithmCard);//查询算法库数据
        IsfdModeCardlVo.ModelCard operatorCard = new IsfdModeCardlVo.ModelCard();
        operatorCard.setDataBaseName("算法库（算子）-结构分析资产");
        operatorCard.setLinkUrl(domain+"/isfd/StrengthDataAssets/algorithm/operator-definition");
        operatorCard.setDataBaseDes("结构分析资产-算法库模块支持定义算法数据结构与模板配置，能够按标准模板导入并解析各类数值方法、网格划分算法、优化算法、AI算法及统计算法等数据。模块不仅提供用户批量导入算法数据的功能，还统一集成管理算法库中的头文件、公式、计算库、函数代码及参数文件，并支持算法库数据的调用查询、批量导入等，从而确保算法资源的高效管理和便捷使用。");
        TableDataInfo<IsfdOperatorVo> operatorPageInfo = iIsfdOperatorService.queryPageList(new IsfdOperatorQueryBo());
        operatorCard.setDataCount(operatorPageInfo.getTotal());
        modelCards.add(operatorCard);
        //查询算法库-数据资产
        IsfdModeCardlVo.ModelCard algorithmCard1 = new IsfdModeCardlVo.ModelCard();
        algorithmCard1.setDataBaseName("算法库-数据资产");
        algorithmCard1.setLinkUrl(domain+"/isfd/DataAssets/algorithm");
        algorithmCard1.setDataBaseDes("数据资产-算法库用于仿真计算全流程涵盖的各类数值方法、网格划分算法、优化算法、AI算法、统计算法等。连续的物理方程离散化为离散的代数方程，利用数值方法进行计算求解，根据物理问题的特性和求解需求，通过自适应网格划分算法，在关键区域增加网格密度。");
        algorithmCard1.setDataCount(getDataCountFromRecord("算法库"));
        modelCards.add(algorithmCard1);
        //查询载荷库数据
        IsfdModeCardlVo.ModelCard loadCard = new IsfdModeCardlVo.ModelCard();
        loadCard.setDataBaseName("载荷库-结构分析资产");
        loadCard.setLinkUrl(domain+"/isfd/StrengthDataAssets/manned-load");
        loadCard.setDataBaseDes("结构分析资产-载荷库允许用户按照标准模板导入并解析结构或系统上的外部作用力和边界条件数据。模块提供批量导入载荷数据的功能，并能将多种格式的载荷数据文件解析为标准模板和结构化数据。");
        TableDataInfo<IsfdLoadVo> loadPageInfo = isfdLoadService.queryPageList(new IsfdLoadQueryBo());
        loadCard.setDataCount(loadPageInfo.getTotal());
        modelCards.add(loadCard);
        //查询算利库数据
        IsfdModeCardlVo.ModelCard arithmeticCard = new IsfdModeCardlVo.ModelCard();
        arithmeticCard.setDataBaseName("算例库-结构分析资产");
        arithmeticCard.setLinkUrl(domain+"/isfd/StrengthDataAssets/arithmetic-example");
        arithmeticCard.setDataBaseDes("结构分析资产-算例库模块主要对特定问题进行建模和分析的具体案例或实例，包含问题的几何形状、材料属性、载荷、初始边界条件、物理场景等计算参数，以及所使用的数值方法和求解策略。");
        TableDataInfo<IsfdCalculationExampleVo> calculationExamplePageInfo = IIsfdCalculationExampleService.queryPageList(new IsfdCalculationExampleBo(), pageQuery);
        arithmeticCard.setDataCount(calculationExamplePageInfo.getTotal());
        modelCards.add(arithmeticCard);
        //查询参数库数据
        IsfdModeCardlVo.ModelCard parameterCard = new IsfdModeCardlVo.ModelCard();
        parameterCard.setDataBaseName("参数库-结构分析资产");
        parameterCard.setLinkUrl(domain+"/isfd/StrengthDataAssets/parameter-library");
        parameterCard.setDataBaseDes("结构分析资产-参数库模块能够按照标准模板导入并解析求解器类型、收敛准则、迭代次数等求解器相关参数，以及时间步长、总仿真时间等时间相关参数。模块不仅支持用户批量导入参数数据，还能够从相关系统集成并采集参数数据，将其解析为结构化数据进行存储。");
        TableDataInfo<IsfdAnalysisParameterConfigVo> parameterConfigs = iIsfdParameterConfigService.getParameterConfigAnalysisPageList(new IsfdAnalysisParameterConfigQueryBo());
        TableDataInfo<IsfdOperatorParameterConfigVo> operatorConfigs = iIsfdParameterConfigService.queryPageParameterConfigOperatorList(new IsfdOperatorParameterConfigQueryBo());
        parameterCard.setDataCount(parameterConfigs.getTotal()+operatorConfigs.getTotal());
        modelCards.add(parameterCard);
        //查询参数库数据
        IsfdModeCardlVo.ModelCard parameterCard1 = new IsfdModeCardlVo.ModelCard();
        parameterCard1.setDataBaseName("参数库-数据资产");
        parameterCard1.setLinkUrl(domain+"/isfd/DataAssets/parameter");
        parameterCard1.setDataBaseDes("数据资产-参数库模块能够按照标准模板导入并解析求解器类型、收敛准则、迭代次数等求解器相关参数，以及时间步长、总仿真时间等时间相关参数。模块不仅支持用户批量导入参数数据，还能够从相关系统集成并采集参数数据，将其解析为结构化数据进行存储。");
        parameterCard1.setDataCount(getDataCountFromRecord("参数库"));
        modelCards.add(parameterCard1);
        //查询型材库数据
        IsfdModeCardlVo.ModelCard profileCard = new IsfdModeCardlVo.ModelCard();
        profileCard.setDataBaseName("型材库-结构分析资产");
        profileCard.setLinkUrl(domain+"/isfd/StrengthDataAssets/profile-library");
        profileCard.setDataBaseDes("结构分析资产-型材库记录所有型材的截面几何参数、铺层、材料和截面等效属性等，并对其进行编号或标准化命名，便于在建模过程中引用。主要来源于标准文件。");
        IIsfdSectionService sectionService = isfdSectionFactory.getSectionServiceByType(IsfdSectionTypeEnum.ALL);
        TableDataInfo<IsfdSectionVo> sectionPageInfo = sectionService.queryPage(new IsfdSectionQueryBo());
        profileCard.setDataCount(sectionPageInfo.getTotal());
        modelCards.add(profileCard);
        //查询型材库数据
        IsfdModeCardlVo.ModelCard profileCard1 = new IsfdModeCardlVo.ModelCard();
        profileCard1.setDataBaseName("型材库-数据资产");
        profileCard1.setLinkUrl(domain+"/isfd/DataAssets/section");
        profileCard1.setDataBaseDes("数据资产-型材库记录所有型材的截面几何参数、铺层、材料和截面等效属性等，并对其进行编号或标准化命名，便于在建模过程中引用。主要来源于标准文件。");
        profileCard1.setDataCount(getDataCountFromRecord("型材库"));
        modelCards.add(profileCard1);
        //查询铺层库数据
        IsfdModeCardlVo.ModelCard layerCard = new IsfdModeCardlVo.ModelCard();
        layerCard.setDataBaseName("铺层库-结构分析资产");
        layerCard.setLinkUrl(domain+"/isfd/StrengthDataAssets/layer-library");
        layerCard.setDataBaseDes("结构分析资产-铺层库记录所有蒙皮、梁腹板、肋腹板等所使用的铺层序列并对其进行编号。");
        IsfdLayupSkinPropertieQueryBo isfdLayupSkinPropertieQueryBo = new IsfdLayupSkinPropertieQueryBo();
        isfdLayupSkinPropertieQueryBo.setPlyCode("");
        isfdLayupSkinPropertieQueryBo.setSymmetrical("");
        TableDataInfo<IsfdLayupSkinPropertieVo> layupSkinPropertiePageInfo = isfdLayupSkinPropertieService.queryPageList(isfdLayupSkinPropertieQueryBo);
        layerCard.setDataCount(layupSkinPropertiePageInfo.getTotal());
        modelCards.add(layerCard);
        //查询铺层库数据
        IsfdModeCardlVo.ModelCard layerCard1 = new IsfdModeCardlVo.ModelCard();
        layerCard1.setDataBaseName("铺层库-复合材料设计");
        layerCard1.setLinkUrl(domain+"/isfd/CompositeMaterial/layup-library");
        layerCard1.setDataBaseDes("复合材料设计-铺层库记录所有复合材料蒙皮、梁腹板、肋腹板等所使用的铺层序列并对其进行编号。");
        TableDataInfo<IsfdLayupSkinPropertieMongoVo> layupSkinPropertieMongo = isfdLayupSkinPropertieMongoService.queryPageList(new IsfdLayupSkinPropertieQueryBo());
        layerCard1.setDataCount(layupSkinPropertieMongo.getTotal());
        modelCards.add(layerCard1);
        //查询区域库数据
        IsfdModeCardlVo.ModelCard fluidArea = new IsfdModeCardlVo.ModelCard();
        fluidArea.setDataBaseName("区域库-流体分析");
        fluidArea.setLinkUrl(domain+"/isfd/fluid/fluid_area");
        fluidArea.setDataBaseDes("流体分析-区域库支持区域数据结构定义和数据模板配置；支持流体系统中几何形状、尺寸、边界条件等数据按照标准模板导入并进行数据解析入库,支持用户手动构建或导入。");
        TableDataInfo<IsfdRegionLibraryMongoVo> isfdRegionLibraryMongo = isfdRegionLibraryMongoService.queryPageList(new IsfdRegionLibraryQueryBo());
        fluidArea.setDataCount(isfdRegionLibraryMongo.getTotal());
        modelCards.add(fluidArea);
        isfdModeCardlVo.setList(modelCards);
        return isfdModeCardlVo;
    }

    /**
     * 先从缓存拿，拿不到再去record里计算
     * 数据资产里面的数量过大，不用实时统计那么全，缓存一天
     */
    private long getDataCountFromRecord(String dataBaseName) {
        String redisKey = RedisConstants.DATA_COUNT_NEXT_PLATFORM_PREFIX + ":" + dataBaseName;
        Object countObject = RedisUtils.getCacheObject(redisKey);
        if (countObject != null) {
            return (Integer) countObject;
        }
        long totalCount = getDataCountFromRecordFromDb(dataBaseName);
        RedisUtils.setCacheObject(redisKey, totalCount, Duration.ofHours(24));
        return totalCount;
    }

    @Override
    public void refreshCache(String dataBaseName){
        String redisKey = RedisConstants.DATA_COUNT_NEXT_PLATFORM_PREFIX + ":" + dataBaseName;
        long totalCount = getDataCountFromRecordFromDb(dataBaseName);
        RedisUtils.setCacheObject(redisKey, totalCount, Duration.ofHours(24));
    }

    private long getDataCountFromRecordFromDb(String dataBaseName){
        List<Long> dataBaseTypeIds = new ArrayList<>();
        //根据名称查询库ID
        IsfdDatabaseTypeManagementVo targetVo = isfdDatabaseTypeManagementMapper.selectVoOne(
            new LambdaQueryWrapper<IsfdDatabaseTypeManagement>()
                .eq(IsfdDatabaseTypeManagement::getDatabaseName, dataBaseName));
        if (targetVo != null) {
            Long topId = targetVo.getTopId();
            List<IsfdDatabaseTypeManagementVo> databaseTypes = isfdDatabaseTypeManagementMapper.selectVoList(
                new LambdaQueryWrapper<IsfdDatabaseTypeManagement>()
                    .in(IsfdDatabaseTypeManagement::getTopId, topId));
            dataBaseTypeIds = databaseTypes.stream()
                .map(IsfdDatabaseTypeManagementVo::getId).toList();
        }

        if (CollectionUtils.isEmpty(dataBaseTypeIds)) {
            return 0;
        }else{
            // 查询数据资产数量
            List<IsfdDatabaseDataImportRecord> records = isfdDatabaseDataImportRecordMapper.selectList(
                new LambdaQueryWrapper<IsfdDatabaseDataImportRecord>()
                    .in(IsfdDatabaseDataImportRecord::getDatabaseTypeId, dataBaseTypeIds));
            //循环查询子记录表数据
            final long[] totalCount = {0};
            List<CompletableFuture<Void>> futures = new ArrayList<>();
            Semaphore semaphore = new Semaphore(THREAD_POOL_SIZE * 2); // 限制最大并发插入的
                try{
                    for (IsfdDatabaseDataImportRecord record : records) {
                        semaphore.acquire(); // 获取许可，控制并发批次数
                        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
                            try {
                                //查询记录子表数据
                                Long importId = record.getId();
                                List<IsfdDatabaseDataImportSubRecord> dataImport = isfdDatabaseDataImportSubMapper.selectList(
                                    new LambdaQueryWrapper<IsfdDatabaseDataImportSubRecord>()
                                        .in(IsfdDatabaseDataImportSubRecord::getImportRecordId, importId));
                                if (dataImport.size() > 0) {
                                    for (IsfdDatabaseDataImportSubRecord subRecord : dataImport) {
                                        //计算模型的数据量
                                        totalCount[0] += (subRecord.getImportQuantity() - subRecord.getDeleteQuantity());
                                    }
                                } else {
                                    //如果没有子表数据，则直接使用记录表数据量
                                    totalCount[0] += record.getDataQuantity();
                                }
                            } catch (Exception e) {
                                log.error("record子记录表查询失败: {}", e.getMessage());
                            } finally {
                                semaphore.release(); // 释放资源
                            }
                        }, threadPoolTaskExecutor);
                        futures.add(future);
                    }
                    CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
                    return totalCount[0];
                } catch (Exception e) {
                    log.error("统计失败 ：" + e.getMessage(), e);
                    return  0;
                }
        }
    }
}
